package com.wgchao.authmanager.domain.model;

import com.wgchao.authmanager.domain.params.MenuResourceCreator;
import com.wgchao.authmanager.domain.params.MenuResourceUpdator;
import com.wgchao.authmanager.infrastructure.exception.ObjectDisabledException;
import com.wgchao.authmanager.infrastructure.exception.ObjectNotMatchingException;
import lombok.Getter;

/**
 * @ClassName: Resource
 * @author: wgchao
 * @createTime: 2020/4/26 4:49 PM
 * @Description: 菜单资源
 */
@Getter
public class MenuResource extends ValueObject{

    private String menuIdentify;

    private String name;

    private String remark;

    public MenuResource(String menuIdentify, String name, String remark) {
        this.menuIdentify = menuIdentify;
        this.name = name;
        this.remark = remark;
    }

    public static MenuResource createMenu(MenuResourceCreator creator){
        return new MenuResource(creator.getMenuIdentify(), creator.getName(), creator.getRemark());
    }

    public void resiveInfo(MenuResourceUpdator updator){
        if(updator.getName() != null){
            this.name = updator.getName().orElse(null);
        }
        if(updator.getRemark() != null){
            this.remark = updator.getRemark().orElse(null);
        }
    }

    @Override
    public int hashCode() {
        int result = menuIdentify != null ? menuIdentify.hashCode() : 0;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if(!(obj instanceof MenuResource)){
            throw new ObjectNotMatchingException("菜单资源类型错误");
        }
        MenuResource menuResource = (MenuResource) obj;
        if(this.menuIdentify.equals(menuResource.menuIdentify)){
            return true;
        }
        return false;
    }
}
